package com.fuershiye.woman.monitor.controller;

import java.util.List;
import java.util.Map;

import com.fuershiye.woman.monitor.service.*;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuershiye.woman.monitor.beans.IdNamePid;
import com.fuershiye.woman.monitor.beans.PageResult;
import com.fuershiye.woman.monitor.beans.Result;
import com.fuershiye.woman.monitor.beans.ViewReportAllValue;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;

/**
 * 地图数据展示
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/map")
public class MapController {

	@Autowired
	AreaService areaService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	DepartmentTypeService departmentTypeService;
	@Autowired
	ReportService reportService;

//	@RequiresPermissions("area:view")
	@RequestMapping(value = "/view.do")
	public String view(String page, Model model) {

		List<Integer> years = DateFormatUtil.getAreaYears();
		model.addAttribute("years", years);

		return "map/" + page;
	}

	
	/**
	 * 接口文档【1.1、按指标和年份，获取17地市，统计局数据】
	 * 同年份,同指标【17地市统计局部门上报数据】
	 * 
	 * @param years	【必传，用英文逗号隔开的年份  2011,2012,2013】
	 * @param indicator【必传】
	 * @return
	 * 
	 * 适用场景【地图展示】
	 */
	// @RequiresPermissions("reportQuery:list")
	@RequestMapping(value = "/list.do")
	@ResponseBody
	public Result mapList(String years, Long indicator, Long formsid) {

		if (StringUtils.isBlank(years) || indicator == null || indicator == 0 || formsid == null || formsid == 0) {
			return new Result(Massage.ERROR_PARAMETER);
		}

		List<Integer> yearList = DateFormatUtil.getYearStr2List(years);
		if(yearList.isEmpty()){
			return new Result(Massage.ERROR_YEAR);
		}

		// 未删除状态数据
		int status = 1;
		// 部门级别 1:国家级;2:省/部级;3:地级市;4:区县级;5:乡镇级;6:其他
		int level = 3;

		// 查找所有的部门
		List<Long> departIds = departmentService.getIdsByCondition(level, 1);
		if (departIds.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		List<Long> indicatorIds = Lists.newArrayList();
		indicatorIds.add(indicator);
		List<ViewReportAllValue> reportList = reportService.getReportDate(departIds, formsid, indicatorIds, yearList, 1);
		if (reportList.isEmpty()) {
			return new Result(Massage.REPORT_IS_NULL);
		}

		List<Map<String, Object>> result = reportService.filter(reportList, yearList);
		
		return new Result(Massage.SUCCESS, new PageResult(result, result.size()));
	}
	
}
